<template>
  <div>
    <van-form>
      <van-cell-group>
        <van-field
          v-model="oldPassword"
          label="旧密码"
          placeholder="请输入旧密码"
          :rules="[{ required: true, message: '请填写旧密码', trigger: 'onBlur' }]"
        />
        <van-field
          v-model="newPassword"
          label="新密码"
          placeholder="请输入新密码"
          :rules="[{ required: true, message: '请填写新密码', trigger: 'onBlur' }]"
        />
        <van-field v-model="rePassword" label="确认密码" placeholder="请重新输入新密码" :rules="rePwdRules" />
      </van-cell-group>
      <div class="btn">
        <van-button round block type="info" @click="confirm">确定</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { updatePassword } from "@/api";
export default {
  data() {
    return {
      oldPassword: "",
      newPassword: "",
      rePassword: "",
      rePwdRules: [
        { required: true, message: "请填确认密码" },
        {
          validator: (value) => {
            return value === this.newPassword;
          },
          message: "密码不一致",
          trigger: "onBlur",
        },
      ],
    };
  },
  methods: {
    async confirm() {
      let { code, message } = await updatePassword(this.newPassword);
      this.$toast(message);
      if (code === 20000) {
        this.$store.commit("setToken", "");
        this.$store.commit("setRefreshToken", "");
        this.$store.commit("setUserInfo", {});
        this.$store.commit("setShopCarList", []);
        this.$router.push("/login");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  margin-top: 16px;
}
</style>
